/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-09-07 15:00:38
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2025-03-31 16:21:18
 */
App({
  // 事件总线
  eventBus: {
    _events: {},
    on(event, callback) {
      if (!this._events[event]) this._events[event] = [];
      this._events[event].push(callback);
    },
    emit(event, data) {
      const callbacks = this._events[event];
      callbacks?.forEach(cb => cb(data));
    },
    off(event, callback) {
      if (callback) {
        this._events[event] = this._events[event]?.filter(cb => cb !== callback);
      } else {
        delete this._events[event];
      }
    }
  },
  onLaunch() {
    this.setNavBarInfo()
    this.getEnvironment()
    this.versionAutoUpdate()
  },
  versionAutoUpdate: function () {
    let that = this
    // 获取小程序更新机制兼容
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      //1. 检查小程序是否有新版本发布
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          //检测到新版本，需要更新，给出提示
          wx.showModal({
            title: '更新提示',
            content: '检测到新版本，是否下载新版本并重启小程序？',
            success: function (res) {
              if (res.confirm) {
                //2. 用户确定下载更新小程序，小程序下载及更新静默进行
                that.downLoadAndUpdate(updateManager)
              } else if (res.cancel) {
                //用户点击取消按钮的处理，如果需要强制更新，则给出二次弹窗，如果不需要，则这里的代码都可以删掉了
                wx.showModal({
                  title: '温馨提示~',
                  content: '本次版本更新涉及到新的功能添加，旧版本无法正常访问的哦~',
                  showCancel: false, //隐藏取消按钮
                  confirmText: "确定更新", //只保留确定更新按钮
                  success: function (res) {
                    if (res.confirm) {
                      //下载新版本，并重新应用
                      that.downLoadAndUpdate(updateManager)
                    }
                  }
                })
              }
            }
          })
        }
      })
    } else {
      // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  /**
   * 下载小程序新版本并重启应用
   */
  downLoadAndUpdate: function (updateManager) {
    wx.showLoading();
    //静默下载更新小程序新版本
    updateManager.onUpdateReady(function () {
      wx.hideLoading()
      //新的版本已经下载好，调用 applyUpdate 应用新版本并重启
      updateManager.applyUpdate()
    })
    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.hideLoading()
      wx.showModal({
        title: '已经有新版本了哟~',
        content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
      })
    })
  },
  // 保存navBar头部高度信息
  setNavBarInfo() {
    let t = this;
    wx.getSystemInfo({
      success: function (res) {
        t.globalData.systemInfo = res;
        let rich = wx.getMenuButtonBoundingClientRect() ? wx.getMenuButtonBoundingClientRect() : null;
        let navBarHeight = 2 * (rich.top - res.statusBarHeight) + rich.height;
        let bottomHeight = res.screenHeight - res.safeArea.bottom;
        t.globalData.navBarHeight = navBarHeight;
        t.globalData.navMarginTop = res.statusBarHeight;
        t.globalData.navHeight = res.statusBarHeight + navBarHeight;
        t.globalData.bottomHeight = bottomHeight
      }
    });
  },
  /**
   * @name: 环境区分
   * @msg: 开发  测试  正式
   * @return {*}
   */
  getEnvironment() {
    const accountInfo = wx.getAccountInfoSync(); //环境区分
    const env = accountInfo.miniProgram.envVersion;
    console.log(env);
    if (env == 'develop') {
      this.globalData.reqUrl = 'https://sc.yuanliuiot.com/sc/app/'
      // this.globalData.reqUrl = 'http://192.168.1.23:1318/app/'
      // this.globalData.reqUrl = 'https://jwa.yuanliuiot.com/sc/app/'
      this.globalData.version = 'v 1.3.1'
      console.log('开发版');
    } else if (env == 'trial') {
      this.globalData.reqUrl = 'https://sc.yuanliuiot.com/sc/app/'
      // this.globalData.reqUrl = 'http://192.168.1.23:1318/app/'
      // this.globalData.reqUrl = 'https://jwa.yuanliuiot.com/sc/app/'
      // this.globalData.reqUrl = 'http://192.168.1.23:1318/app/'
      this.globalData.version = 'v 1.3.1 测试版'
      console.log('测试版');
    } else if (env == 'release') {
      this.globalData.reqUrl = 'https://jwa.yuanliuiot.com/sc/app/'
      this.globalData.version = 'v 1.3.1'
    } else {
      wx.showToast({
        title: '网络错误',
        icon: none
      })
    }
  },

  globalData: {
    navBarHeight: '', //导航栏高度
    navMarginTop: '', //导航栏距离顶部高度
    navHeight: '', //导航栏+导航栏距离顶部高度
    bottomHeight: '', //iphonex，xr，12等机型底部距离

    reqUrl: '', //请求地址
    version: '', //版本号

    isBtnsAlarm: true, //告警处理处理按钮的显示隐藏

    userId: wx.getStorageSync('userId') || '', //用户id
    rolename: wx.getStorageSync('rolename') || '', //角色名称
    token: wx.getStorageSync('token') || '',
    mobile: wx.getStorageSync('mobile') || '', //手机号
    nickname: wx.getStorageSync('nickname') || '', //昵称
    tenantId: wx.getStorageSync('tenantId') || '',
    username: wx.getStorageSync('username') || '', //用户名
    idCard: wx.getStorageSync('idCard') || '', //身份证

    openId: wx.getStorageSync('openId') || '',
    wxNickName: wx.getStorageSync('wxNickName') || '',

    orgId: wx.getStorageSync('orgId') || '', //1==选择全部单位
    orgId2: wx.getStorageSync('orgId2') || '', //tenantId等于0的为个人模式，不等于0的为企业模式。页面做区分，参考铠湾。
    orgName: wx.getStorageSync('orgName') || '',
    flatType: wx.getStorageSync('flatType') || '',

    descriptionRole: wx.getStorageSync('descriptionRole') || '',

    isTeamManager: wx.getStorageSync('isTeamManager') || false, //是否为单位管理员
    isAlarm: wx.getStorageSync('isAlarm') || false, //是否从告警处理记录出来
    isProprietor: wx.getStorageSync('isProprietor') || false, //业主==true 装维==false

    unitLevelList: wx.getStorageSync('unitLevelList') || null,
    orgInfo: wx.getStorageSync('orgInfo') || null,

    familyInfo: wx.getStorageSync('familyInfo') || null, //家庭信息

    isUpdateCIndex :wx.getStorageSync('isUpdateCIndex') || false, //更新c端首页
    deviceInfo :wx.getStorageSync('deviceInfo') || null, //设备信息
  },
})